// pages/login/login.js

const request = require('../../utils/request.js');
const util = require('../../utils/util.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 当前登录选项
    current: 1,
    codeText: '获取验证码',
    //验证码倒数秒
    counting: false,
    //是否密文显示 true为密文， false为明文
    ispassword: true,
    //手机号
    phone: "",
    //登录密码
    pwd: "",
    //手机验证码
    phone_code: ""
  },

  // 切换登陆注册监听
  switchLogin(e) {
    let index = e.currentTarget.dataset.code;
    this.setData({
      current: index,
      phone: "",
      pwd: "",
      phone_code: ""
    })
  },
  /**
   * 输入手机号
   */
  inputPhone(e) {
    var phoneNum = e.detail.value.replace(/\s+/g, '');
    this.setData({
      phone: phoneNum
    })
  },
  /**
   * 输入密码
   */
  inputPwd(e) {
    var loginPwd = e.detail.value.replace(/\s+/g, '');
    this.setData({
      pwd: loginPwd
    })
  },
  /**
   * 转换密码输入框是否明文显示
   */
  switchPwd() {
    this.setData({
      ispassword: !this.data.ispassword
    })
  },
  /**
   * 输入验证码
   */
  inputPhoneCode(e) {
    var phoneCode = e.detail.value.replace(/\s+/g, '');
    this.setData({
      phone_code: phoneCode
    })
  },

  /**
   * 账号密码登录
   */
  pwdLogin() {
    var that = this;
    var phoneNum = that.data.phone;
    var loginPwd = that.data.pwd;
    if (util.isStrEmpty(phoneNum)) {
      util.showToast("手机号不能为空", "error");
      return;
    }
    if (that.data.phone.length != 11) {
      util.showToast("手机号格式错误", "error");
      return;
    }
    if (util.isStrEmpty(loginPwd)) {
      util.showToast("密码不能为空", "error");
      return;
    }
    var mydata = {
      phone: phoneNum,
      pwd: loginPwd
    }
    request.post('/access/loginUser', mydata).then((res) => {
      if (res.code == 200) {
        wx.setStorageSync('token', res.data.token);
        wx.setStorageSync('userInfo', res.data);
        that.bindWx(); //绑定微信
        wx.switchTab({
          url: '/pages/index/index',
        })
      } else {
        util.showToast(res.msg, 'none');
      }
    }).catch((errMsg) => {
      util.showToast(errMsg, 'error');
    })
  },
  /**
   * 绑定微信
   */
  bindWx() {
    var that = this;
    wx.login({
      success(res) {
        // console.log(res);
        let code = res.code;
        //通过code进行登录
        var mydata = {
          code: code
        }
        request.post('/user/bindAccountToWX', mydata).then((res) => {
          // util.showToast(res.msg,'none');
        }).catch((errMsg) => {
          // util.showToast(errMsg,'error');
        })
      },
      fail(res) {
        wx.showToast({
          title: '授权失败！',
          icon: 'error'
        })
      }
    })
  },


  /**
   * 微信登陆
   */
  wxLogin() {
    var that = this;
    wx.login({
      success(res) {
        // console.log(res);
        let code = res.code;
        //通过code进行登录
        var mydata = {
          code: code
        }
        request.post('/access/loginUserAtWMP', mydata).then((res) => {
          if (res.code == 200) {
            wx.setStorageSync('token', res.data.token);
            wx.setStorageSync('userInfo', res.data);
            wx.switchTab({
              url: '/pages/index/index',
            })
          } else if (res.code == 204) {
            util.showToast(res.msg, 'none');
            that.setData({
              current: 2,
              phone: "",
              pwd: "",
              phone_code: ""
            })
          } else {
            util.showToast(res.msg, 'none');
          }
        }).catch((errMsg) => {
          util.showToast(errMsg, 'error');
        })
      },
      fail(res) {
        wx.showToast({
          title: '授权失败！',
          icon: 'error'
        })
      }
    })
  },
  /**
   * 获取验证码 
   */
  getCode() {
    var that = this;
    if (!that.data.counting) {
      if (util.isStrEmpty(that.data.phone) || that.data.phone.length != 11) {
        util.showToast("手机号输入有误", 'error');
        return;
      }
      var mydata = {
        phone: that.data.phone,
      }
      request.get('/access/getMsgCode', mydata).then((res) => {
        if (res.code == 200) {
          wx.showToast({
            title: '验证码已发送',
          })
          //开始倒计时60秒
          that.countDown(that, 60);
        } else {
          util.showToast(res.msg, 'none');
        }
      }).catch((errMsg) => {
        util.showToast(errMsg, 'error');
      })
    }
  },
  /**
   * 倒计时60秒
   */
  countDown(that, count) {
    if (count == 0) {
      that.setData({
        codeText: '获取验证码',
        counting: false
      })
      return;
    }
    that.setData({
      counting: true,
      codeText: count + '秒后重新获取',
    })
    setTimeout(function () {
      count--;
      that.countDown(that, count);
    }, 1000);
  },
  /**
   * 手机验证码登录
   */
  phoneMsgLogin() {
    var that = this;
    var phoneNum = that.data.phone;
    var msgCode = that.data.phone_code;
    if (util.isStrEmpty(phoneNum)) {
      util.showToast("手机号不能为空", "error");
      return;
    }
    if (that.data.phone.length != 11) {
      util.showToast("手机号格式错误", "error");
      return;
    }
    if (util.isStrEmpty(msgCode)) {
      util.showToast("验证码不能为空", "error");
      return;
    }
    var mydata = {
      phone: phoneNum,
      code: msgCode
    }
    request.post('/access/msgCodeLogin', mydata).then((res) => {
      if (res.code == 200) {
        wx.setStorageSync('token', res.data.token);
        wx.setStorageSync('userInfo', res.data);
        that.bindWx();
        wx.switchTab({
          url: '/pages/index/index',
        })
      } else {
        util.showToast(res.msg, 'none');
      }
    }).catch((errMsg) => {
      util.showToast(errMsg, 'error');
    })
  },

  /**
   * 重置密码
   */
  resetPwd() {
    var that = this;
    var phoneNum = that.data.phone;
    var loginPwd = that.data.pwd;
    var msgCode = that.data.phone_code;
    if (util.isStrEmpty(phoneNum)) {
      util.showToast("手机号不能为空", "error");
      return;
    }
    if (that.data.phone.length != 11) {
      util.showToast("手机号格式错误", "error");
      return;
    }
    if (util.isStrEmpty(loginPwd)) {
      util.showToast("密码不能为空", "error");
      return;
    }
    if (util.isStrEmpty(msgCode)) {
      util.showToast("请填入验证码", "error");
      return;
    }

    var mydata = {
      phone: phoneNum,
      newPwd: loginPwd,
      code: msgCode
    }
    request.post('/access/resetUserPwd', mydata).then((res) => {
      if (res.code == 200) {
        util.showToast(res.msg, 'success');
        that.setData({
          current: 2,
          phone: "",
          pwd: "",
          phone_code: ""
        })
      } else {
        util.showToast(res.msg, 'none');
      }
    }).catch((errMsg) => {
      util.showToast(errMsg, 'error');
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    //检测本地是否还保存着登录状态时的个人信息，可直接免登录
    if (wx.getStorageSync('token') != '') {
      //跳转到主页
      wx.switchTab({
        url: '/pages/index/index',
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})